package com.example.demo.service;

import com.example.demo.mapper.ArticleMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.TestTable;
import com.example.demo.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.nio.ch.ThreadPool;

import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-08-10
 * Time: 0:26
 */
@Service
public class TestService {

    @Autowired
    private UserMapper userMapper;

    public List<UserInfo> getAll() {
        return userMapper.getAll();
    }

    public UserInfo getOne(Integer id) {
        return id == null ? null : userMapper.getUserById(id);
    }
    public UserInfo getOneByUsername(String username) {
        return username == null ? null : userMapper.getUserByUsername(username);
    }

    public List<UserInfo> getAll2() {
        return userMapper.getAll2("");
    }

    public UserInfo login(String username, String password) {
        return userMapper.login(username, password);
    }

    public int update(UserInfo userInfo) {
        return userMapper.update(userInfo);
    }

    public int delete(Integer state) {
        return userMapper.delete(state);
    }

    public int insert(UserInfo userInfo) {
        int rows = userMapper.insert(userInfo);
        int id = userInfo.getId();
        System.out.println("rows: " + rows + "  id: " + id);
        return rows;
    }

    public List<TestTable> getDesc() {
        return userMapper.getT();
    }

    public List<UserInfo> getLike(String likeString) {
        return userMapper.getAllLikeSome(likeString);
    }

    @Autowired
    private ArticleMapper articleMapper;
    public List<ArticleInfo> getAll3() {
        return articleMapper.getAll3();
    }

//    public UserInfo getArticles(Integer id) {
//        //1. 通过id找用户
//        UserInfo userInfo = userMapper.getUserById(id);
//        //2. 通过id找文章
//        List<ArticleInfo> artList = articleMapper.getAllByUid(id);
//        //3. 合并表的结果
//        userInfo.setArtList(artList);
//        return userInfo;
//    }

    public UserInfo getArticles(Integer id) {
        //工厂类获取线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(2);

        //JUC（CountDownLatch）的方式
        CountDownLatch countDownLatch = new CountDownLatch(2);

        Object[] objects = new Object[2];
        threadPool.submit(() -> {
            //1. 通过id找用户
            objects[0] = userMapper.getUserById(id);
            countDownLatch.countDown();
        });
        threadPool.submit(() -> {
            //2. 通过id找文章
            objects[1] = articleMapper.getAllByUid(id);
            countDownLatch.countDown();
        });

        // 等待两个线程结束才继续后面的内容
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //3. 合并表的结果
        UserInfo userInfo = (UserInfo) objects[0];
        userInfo.setArtList((List<ArticleInfo>) objects[1]);
        return userInfo;
    }

    public Integer add(UserInfo userInfo) {
//        return userMapper.add(userInfo.getUsername(), userInfo.getPassword(), userInfo.getState(), userInfo.getId());
        return userMapper.add2(userInfo);
    }

    public List<UserInfo> select1(UserInfo userInfo) {
        return userMapper.select1(userInfo);
    }
    public Integer update2(UserInfo userInfo) {
        return userMapper.update2(userInfo);
    }

    public Integer delete2() {
        List<Integer> list = new ArrayList<Integer>() {
            {
                //匿名内部类 + 实例代码块的原理
                add(100);
                add(101);
                add(102);
                add(103);
                add(104);
            }
        };
        return userMapper.deleteByIDs(list);
    }

    public Integer insertUsers() {
        UserInfo userInfo1 = new UserInfo("测试人1", "123", 1);
        UserInfo userInfo2 = new UserInfo("测试人2", "456", 1);
        UserInfo userInfo3 = new UserInfo("测试人3", "789", 0);
        List<UserInfo> list = new ArrayList<UserInfo>() {
            {
                add(userInfo1);
                add(userInfo2);
                add(userInfo3);
            }
        };
        List<UserInfo> list2 = new ArrayList<UserInfo>() {
            {
                add(userInfo3);
                add(userInfo2);
                add(userInfo1);
            }
        };
        return userMapper.insertByUsers(list, list2);
    }


}
